import sysConfig from '@/common/config/config.js'

class SocketTask {
	constructor() {
		this.socketUrl = sysConfig.congifService.socketUrl+'?Authorization=Bearer '+uni.getStorageSync('token');
		this.socketStart = false;
		this.socketReceive();
	}
	init() {
		if (this.socketStart) {
			console.log("已启动WebSocket连接");
		} else {
			console.log("开始连接WebSocket");
			let url = this.socketUrl;
			uni.connectSocket({
				url: url,
				method: 'GET',
			});
			uni.onSocketOpen((res) => {
				this.socketStart = true;
				console.log("WebSocket连接成功");
			})

		}
	}
	socketReceive() {
		uni.onSocketMessage((res) => {
			let badge = uni.getStorageSync('badge');
			if(badge>0){
				badge+=1;
				uni.setStorageSync('badge', badge);
			}else{
				uni.setStorageSync('badge', 1);
			}
			
		});
	}
	closeSocket() {
		uni.closeSocket();
		console.log("已关闭WebSocket");
	}

};
const webSocket = new SocketTask();
export default webSocket;